var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var app = getApp();

Page({
 data: {
  banner: [],
  mcategoryList:[],
  mechanismList:[],
  top3:[]
 },
 onLoad: function(options) {
  var that = this
  this.getCatalog();
 },
 onPullDownRefresh() {
  wx.showNavigationBarLoading() //在标题栏中显示加载
  this.getCatalog();
  wx.hideNavigationBarLoading() //完成停止加载
  wx.stopPullDownRefresh() //停止下拉刷新
 },
  getCatalog: function () {
    let that = this;
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        util.request(api.ServiceIndex, {
          longitude: res.longitude,
          latitude: res.latitude
        }).then(function (res) {
          if (res.errno === 0) {
            that.setData({
              mcategoryList: res.data.mcategoryList,
              mechanismList: res.data.mechanismList,
              banner: res.data.banner,
              top3: res.data.top3
            });
            // console.log(res.data.mcategoryList);
            // console.log(res.data.mechanismList);
            // console.log(res.data.banner);
            // console.log(res.data.top3)
          }
        });
      }
    });
  },
 getCurrentCategory: function(id) {
  let that = this;
  util.request(api.CatalogCurrent, {
    id: id
   })
   .then(function(res) {
    that.setData({
     currentCategory: res.data.currentCategory,
     currentSubCategoryList: res.data.currentSubCategory
    });
   });
 },
  goVoucher: function(){
    if (app.globalData.hasLogin) {
      wx.navigateTo({
        url: "/pages/ucenter/voucher/voucher"
      });
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    }
  },
 onReady: function() {
 },
 onShow: function() {
  // 页面显示
 },
 onHide: function() {
  // 页面隐藏
 },
 onUnload: function() {
  // 页面关闭
 },
 getList: function() {
  var that = this;
  util.request(api.ApiRootUrl + 'api/catalog/' + that.data.currentCategory.catId)
   .then(function(res) {
    that.setData({
     categoryList: res.data,
    });
   });
 },
 switchCate: function(event) {
  var that = this;
  var currentTarget = event.currentTarget;
  if (this.data.currentCategory.id == event.currentTarget.dataset.id) {
   return false;
  }

  this.getCurrentCategory(event.currentTarget.dataset.id);
 }
})